package ch.lue.tvsedit;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Menu;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetListener;
import java.awt.event.ActionEvent;
import java.io.IOException;

import javax.swing.AbstractAction;
import javax.swing.AbstractListModel;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.table.TableModel;

import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

import ch.lue.tvsedit.model.TVSModel;
import ch.lue.tvsedit.swing.ListListModel;

public class TVSEdit {
	private static DefaultListModel groupModel;
	private static TableModel channelModel;


	public static void main(String[] args) throws SAXException, IOException {
		XMLReader parser = XMLReaderFactory.createXMLReader();

		TVSModel handler = new TVSModel();
		parser.setContentHandler(handler);

		parser.parse("export1.xml");

		groupModel = handler.getGroupModel();
		channelModel = handler.getChannelModel();

		
		SwingUtilities.invokeLater(new Runnable() {
		    public void run() {
		        createAndShowGUI();
		    }

		});
	}
	

	private static void createAndShowGUI() 
	{
		JFrame frame = new JFrame("TV Editor");
		
		JMenuBar bar = new JMenuBar();
		bar.add(createMenu());
		frame.setJMenuBar(bar);
		
		JList jGroupList = new JList(groupModel);
		JTable jChannelList = new JTable(channelModel);
		ListListModel listListModel = new ListListModel(jGroupList.getSelectionModel(), jGroupList);
		JList jChanOrder = new JList(listListModel);
		jChanOrder.setDragEnabled(true);
		
		jGroupList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		ListRenderer renderer = new ListRenderer();
		jGroupList.setCellRenderer(renderer);
		
		
		
		Container content = frame.getContentPane();
		content.setLayout(new BorderLayout());
		content.add(new JScrollPane(jChannelList), BorderLayout.NORTH);
		content.add(new JScrollPane(jGroupList), BorderLayout.WEST);
		content.add(new JScrollPane(jChanOrder), BorderLayout.CENTER);
		
		content.add(new JScrollPane(new JButton(new AddChannelsAction(jChannelList, jGroupList))), BorderLayout.SOUTH);
		
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}


	private static JMenu createMenu() 
	{
		JMenu jmenu = new JMenu("File");
		JMenuItem open = new JMenuItem(new AbstractAction("open") {
			@Override
			public void actionPerformed(ActionEvent e) 
			{
				SwingUtilities.invokeLater(new Runnable() {
				    public void run() {
				    }

				});
			}
		});
		
		JMenuItem close = new JMenuItem(new AbstractAction("close") {
			@Override
			public void actionPerformed(ActionEvent e) 
			{
				SwingUtilities.invokeLater(new Runnable() {
				    public void run() {
				    }

				});
			}
		});
		
		jmenu.add(open);
		jmenu.add(close);
		
		return jmenu;
	}
}
